﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using EShop.Common;
using EShop.BussinessObjects;
using MySql.Data.MySqlClient;

namespace EShop.DataAccess
{
    public class CDACompanyGallery
    {
        public static List<CompanyGallery> Select(CompanyGallery companyGallery)
        {
            MySqlConnection con = new MySqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
            con.Open();

            string NotEffective=((int)cnstDatabase.DBOperationResult.ValueIsNotEffective).ToString();

            string _cmdText =
            " SELECT c.* " +
            " FROM companyGallery c " +
            " WHERE ((c.ID = " + companyGallery.ID.ToString() + ") OR (" + companyGallery.ID.ToString() + " = " + NotEffective + ")) " +
            "AND" + "((c.CompanyID=" + companyGallery.Company.ID.ToString() + ")OR(" + companyGallery.Company.ID.ToString() + "=" + NotEffective + "))" +
            "ORDER BY c.ID";

            MySqlCommand cmd = new MySqlCommand(_cmdText, con);
            MySqlDataAdapter dataAdapter = new MySqlDataAdapter(cmd);
            DataTable tbl = new DataTable();
            dataAdapter.Fill(tbl);


            List<CompanyGallery> companyGalleryList = new List<CompanyGallery>();

            CompanyGallery _companyGallery;
            foreach (DataRow row in tbl.Rows)
            {
                _companyGallery = new CompanyGallery();

                _companyGallery.ID = (row["ID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["ID"]));
                _companyGallery.Company = new Company();
                _companyGallery.Company.ID = (row["CompanyID"].ToString() == "" ? ((int)cnstDatabase.DBOperationResult.NullValue) : Convert.ToInt32(row["CompanyID"]));

                _companyGallery.Name = (row["Name"].ToString() == "" ? null : row["Name"].ToString());

                companyGalleryList.Add(_companyGallery);
            }

            con.Close();
            return companyGalleryList;
        }
    }
}